package com.kuaiyin.controller.user;

import com.kuaiyin.dto.UserDTO;
import com.kuaiyin.entity.User;
import com.kuaiyin.result.Result;
import com.kuaiyin.service.IUserService;
import com.kuaiyin.utils.PageResult;
import com.kuaiyin.vo.UserDetailVO;
import com.kuaiyin.vo.UserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户基础信息表（使用逻辑外键） 前端控制器
 * </p>
 *
 * @author ng
 * @since 2025-04-21
 */
@Tag(name = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    /**
     * 根据id查询用户信息
     *
     * @param id 用户id
     * @return 用户信息
     */
    @Operation(summary = "根据id查询用户信息")
    @Cacheable(cacheNames = "users", key = "#id")
    @GetMapping("/{id}")
    public Result<UserDetailVO> getUserById(@PathVariable("id") Long id) {
        UserDetailVO UserdetailVO = userService.getById(id);
        return Result.success(UserdetailVO);
    }

    @Operation(summary = "新增用户")
    @CacheEvict(cacheNames = "users", allEntries = true)
    @PostMapping()
    public Result<String> saveUser(@Valid @RequestBody UserDTO userDTO) {
        userService.saveUser(userDTO);
        return Result.success();
    }

    @Operation(summary = "更新用户")
    @CacheEvict(cacheNames = "users", allEntries = true)
    @PutMapping("/{id}")
    public Result<String> updateUser(@PathVariable Long id, @RequestBody UserDTO userDTO) {
        userService.updateUser(id, userDTO);
        return Result.success();
    }

    @Operation(summary = "删除用户")
    @CacheEvict(cacheNames = "users", allEntries = true)
    @DeleteMapping("/{id}")
    public Result<String> deleteUser(@PathVariable Long id) {
        userService.removeById(id);
        return Result.success();
    }

    @Operation(summary = "查询用户列表")
    @Cacheable(cacheNames = "users", key = "'all'")
    @GetMapping("/all")
    public Result<List<UserVO>> getUserList() {
        List<UserVO> userVOList = userService.getUserList();
        return Result.success(userVOList);
    }

    @Operation(summary = "更新用户状态")
    @CacheEvict(cacheNames = "users", allEntries = true)
    @PutMapping("/{id}/status/{status}")
    public Result<String> updateUserStatus(@PathVariable Long id, @PathVariable Integer status) {
        userService.updateUserStatus(id, status);
        return Result.success();
    }

    @Operation(summary = "分页查询用户列表")
    @GetMapping("/search")
    public Result<PageResult<UserVO>> getUsers(
            @RequestParam(required = false) String username,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        PageResult<UserVO> userPage = userService.getUserPage(page, size, wrapper -> {
            if (StringUtils.hasText(username)) {
                wrapper.like(User::getUsername, username);
            }
            wrapper.orderByDesc(User::getId);
        });
        return Result.success(userPage);
    }
}
